<!-- 保险公司编辑弹窗 -->
<template>
  <ele-modal
    form
    :width="500"
    :model-value="modelValue"
    :title="title"
    @update:modelValue="updateModelValue"
  >
    <el-form ref="formRef" :model="form" :rules="rules" label-width="130px">
      <el-form-item label="退保金额">
        {{ form.refund_amount }}
      </el-form-item>
      <el-form-item label="退还佣金">
        {{ form.refund_rebate }}
      </el-form-item>
      <el-form-item label="退还车主金额" prop="refund_to_driver_amount">
        <el-input v-model="form.refund_to_driver_amount" style="width: 200px" />
      </el-form-item>
      <el-form-item label="付款图片" prop="refund_to_driver_images">
        <image-upload :limit="99" v-model="form.refund_to_driver_images" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="updateModelValue(false)">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script setup>
  import { ref, reactive, watch } from 'vue';
  import { EleMessage } from 'ele-admin-plus/es';
  import { useFormData } from '@/utils/use-form-data';
  import { doneInsuranceRefundToDriver } from '@/api/company';
  import ImageUpload from '@/components/ImageUpload/index.vue';

  const emit = defineEmits(['done', 'update:modelValue']);

  const props = defineProps({
    /** 弹窗是否打开 */
    modelValue: Boolean,
    /** 修改回显的数据 */
    data: Object
  });

  /** 是否是修改 */
  const isUpdate = ref(false);

  /** 提交状态 */
  const loading = ref(false);

  /** 表单实例 */
  const formRef = ref(null);

  /** 表单数据 */
  const [form, resetFields, assignFields] = useFormData({
    id: 0,
    refund_amount: '',
    refund_rebate: '',
    insurance_id: 0,
    refund_to_driver_amount: '',
    refund_to_driver_images: '' // 付款图片
  });

  /** 表单验证规则 */
  const rules = reactive({
    refund_to_driver_images: [
      {
        required: true,
        message: '请上传付款图片',
        type: 'string',
        trigger: 'blur'
      }
    ],
    refund_to_driver_amount: [
      {
        required: true,
        message: '请填写退款金额',
        type: 'string',
        trigger: 'blur'
      }
    ]
  });

  /** 保存编辑 */
  const save = () => {
    console.log(form);
    formRef.value?.validate?.((valid) => {
      if (!valid) {
        return;
      }

      loading.value = true;
      var requestData = Object.assign({}, form);
      doneInsuranceRefundToDriver(requestData)
        .then((msg) => {
          loading.value = false;
          EleMessage.success(msg);
          updateModelValue(false);
          emit('done');
        })
        .catch(() => {
          loading.value = false;
        });
    });
  };

  /** 更新modelValue */
  const updateModelValue = (value) => {
    emit('update:modelValue', value);
  };
  const title = ref('');
  watch(
    () => props.modelValue,
    (modelValue) => {
      if (modelValue) {
        title.value = props.data.plate_number + '的退保返点';
        if (props.data) {
          assignFields({
            ...props.data
          });
          isUpdate.value = true;
        } else {
          isUpdate.value = false;
        }
      } else {
        resetFields();
        formRef.value?.clearValidate?.();
      }
    }
  );
</script>
